/**
 * All rights reserved by www.xmerge.cn
 */
package net.xfok.info.nba;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

/**
 * @author YangHua
 *
 */
public class InfoParser {
	static final String TAG = "InfoParser";
	
    static final String NODE_INFO_ITEM = "info";
    static final String NODE_INFO_TITLE = "title";
    static final String NODE_INFO_ID = "id";
    static final String NODE_INDO_DATE = "date";
    
    static final Set<String> fetchChars = new HashSet<String>();

    static {
        fetchChars.add(NODE_INFO_ITEM);
        fetchChars.add(NODE_INFO_TITLE);
        fetchChars.add(NODE_INFO_ID);
        fetchChars.add(NODE_INDO_DATE);
    }
	      
    private List<InfoItem> infoItems;
    private InfoItem currentItem = null;
    private StringBuilder cache = new StringBuilder(4096);
    
//    public InfoParser(String url){
//    	infoItems=this.getInfoItems(url);
//    }
    
    public List<InfoItem> getInfoItems() {
    	return this.infoItems;
    }
    
//    private List<InfoItem> getInfoItems(String url) {
//    	InputStream is=InfoConstant.post(url);
//    	if(is!=null){
//    		try{
//    			infoItems=new ArrayList<InfoItem>();
//    			InfoParserHandler handler=new InfoParserHandler();
//    			SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
//    			SAXParser parser = saxParserFactory.newSAXParser();
//    			Log.i(TAG, "parse start");
//    			parser.parse(is, handler);
//    			Log.i(TAG, "parse end");
//    		}catch(Exception e){
//    			e.printStackTrace();
//    		}
//    	}
//    	return infoItems;
//    }
  
	private class InfoParserHandler extends DefaultHandler {
		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			if(cache!=null){
				 cache.append(ch, start, length);
			}
		}
		
		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attr) throws SAXException {		
			if(localName.equals(NODE_INFO_ITEM)){
				currentItem = new InfoItem();
			}
			if (fetchChars.contains(localName)){
				cache = new StringBuilder(1024);
			}else{
				cache = null;
			}
		}
		
		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
			if(localName.equals(NODE_INFO_ITEM)){
	            infoItems.add(currentItem);
	            currentItem = null;
	        }
            if (currentItem!=null&&cache!=null) {
                if (NODE_INFO_TITLE.equals(localName)){
                    currentItem.setTitle(cache.toString());
                }
                if (NODE_INFO_ID.equals(localName)){
                    currentItem.setInfoId(cache.toString());
                }              
                if (NODE_INDO_DATE.equals(localName)){
                    currentItem.setDate(cache.toString());
                }      
            }
		}
	}
}
